﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using mamnontuoitho7.AppCode;
using System.Data;
using System.Text.RegularExpressions;

namespace mamnontuoitho7.module
{
    public partial class KidWallView : System.Web.UI.Page
    {

        protected Kid _kid = new Kid();
        protected Teacher _teacher = new Teacher();
        protected General _general = new General();
        protected KidWall _kidWall = new KidWall();
        protected DataRow _dtrKid;

        protected DataTable _dtWall;
        protected string _kidID;
        protected int _pageflag;
        protected int _currentbar = 1;

        protected int _totalPages;
        protected int _totalWallNumber;
        protected int _totalBars;

        protected int _beginPage;// dung bên trang giao diện
        protected int _endPage;// dung bên trang giao diện

        protected static bool _checkValidKid;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["memberID"].ToString() != "" && (Session["role"].ToString() == "3" || Session["role"].ToString() == "4"))
            {

                    try
                    {
                        if (Request.Form["hidWallIDDelete"] != null)
                        {
                            string _wallID = Request.Form["hidWallIDDelete"].ToString();
                            _kidWall.WallDelete(_wallID);
                        }

                        if (Request.QueryString["KidID"] != null)
                        {
                           // Session["KidWallView_kidID"] = Request.QueryString["KidID"].ToString();
                            _kidID = Request.QueryString["KidID"].ToString();
                        }

                        if (_kidID != null && _kidID != "")
                        {
                            Match match = Regex.Match(_kidID, @"([0-9])$");
                            //If kidID is greater than 6 character
                            if (_kidID.Length > 6 || !match.Success)
                            {
                                return;
                            }

                            //Check Teacher or Parent view kid details
                            if (Session["role"].ToString() == "3")
                            {
                                _dtrKid = _kid.GetKidInformationOfClass(_kidID, Session["classID"].ToString());
                            }
                            else if (Session["role"].ToString() == "4")
                            {
                                _dtrKid = _kid.GetKidInformationOfParent(_kidID, Session["memberID"].ToString());
                                _kidWall.ChangeWallStatus(Session["memberID"].ToString(), _kidID);
                            }
                            if (_dtrKid == null)
                            {
                                _checkValidKid = false;
                                return;
                            }
                            _checkValidKid = true;

                            ////////////////////start paging NOTICE
                            _totalWallNumber = _kidWall.TotalWallOfOneKid(_kidID).GetValueOrDefault();
                            _totalPages = _general.Divide(_totalWallNumber, ConstantClass._pageStepWall);
                            _totalBars = _general.Divide(_totalPages, ConstantClass._barStepWall);

                            if (Request.QueryString["page"] != null)// khi bấm vào các ô số
                            {
                                string temp = Request.QueryString["page"].ToString();
                                Match match2 = Regex.Match(temp, @"([0-9])$", RegexOptions.IgnoreCase);

                                if (temp.Length <= 4 && match2.Success)
                                {
                                    _pageflag = Convert.ToInt16(temp);// trang hiện hành
                                    if (_pageflag <= _totalPages)
                                    {
                                        _currentbar = _general.Divide(_pageflag, ConstantClass._barStepWall);
                                        Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                                    }
                                    else
                                    {
                                        _pageflag = 1;
                                        _currentbar = 1;
                                        Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                                    }
                                }
                            }
                            if (Request.QueryString["page"] == null && Request.QueryString["a"] == null)// khi mới vào trang
                            {
                                _currentbar = 1;
                                _pageflag = 1;
                                Response.Cookies["CurrentBar"].Value = "1";
                            }

                            if (Request.QueryString["a"] != null && Request.QueryString["a"] == "1") // khi bấm vào ô >> (next)
                            {
                                _currentbar = Int16.Parse(Request.Cookies["CurrentBar"].Value);
                                if (_currentbar < _totalBars)
                                {
                                    _currentbar += 1;
                                    Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                                }
                                _pageflag = (_currentbar - 1) * ConstantClass._barStepWall + 1;

                            }

                            if (Request.QueryString["a"] != null && Request.QueryString["a"] == "-1") // khi bấm vào ô << (previous)
                            {
                                _currentbar = Int16.Parse(Request.Cookies["CurrentBar"].Value);
                                if (_currentbar > 1)
                                {
                                    _currentbar -= 1;
                                    Response.Cookies["CurrentBar"].Value = _currentbar.ToString();
                                }
                                _pageflag = (_currentbar - 1) * ConstantClass._barStepWall + 1;
                            }

                            _dtWall = _kidWall.GetWallPaged(_pageflag, ConstantClass._pageStepWall, _kidID);
                            _beginPage = (_currentbar - 1) * ConstantClass._barStepWall + 1;
                            _endPage = _currentbar * ConstantClass._barStepWall;

                            if (_endPage > _totalPages)
                            {
                                _endPage = _totalPages;
                            }
                            //////////////////////////////End Paging NOTICE
                        }
                        else
                        {
                            Response.Redirect(ConstantClass._errorLoginPage);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                        Response.Redirect(ConstantClass._errorLoginPage);
                    }

            }
            else
            {
                Response.Redirect(ConstantClass._errorLoginPage);
            }

            Session["Common_articleTypeID"] = "1";
        }


        protected void btnWallSend_Click(object sender, EventArgs e)
        {
            try
            {
                bool check = _kidWall.KidWallCreate(Session["memberID"].ToString(), _kidID, txtWall.Text.TrimEnd());
                txtWall.Text = "";
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Response.Redirect(ConstantClass._errorLoginPage);
            }
            Response.Redirect("KidWallView.aspx?KidID="+_kidID);
        }
    }
}